//
//  ChatTViewController.m
//  SanYiTeaching
//
//  Created by ChenHao on 2019/8/7.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "ChatTViewController.h"
#import "SYTListViewController.h"
#import "RYChatViewController.h"
#import "RYChatBaseView.h"

@interface ChatTViewController ()
{
    RYChatViewController *chatVC;
}
@end

@implementation ChatTViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //覆盖导航栏view
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 215, 65)];
    //    view.backgroundColor = [UIColor colorWithRed:228/255 green:228/255 blue:228/255 alpha:1];
    view.backgroundColor = [UIColor colorWithHexString:@"#E4E4E4"];
    [self.view addSubview:view];
    
    //搜索框加最近联系人的整体view
    UIView* searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 55, 215, 65)];
    searchView.backgroundColor = [UIColor colorWithHexString:@"#E4E4E4"];
    
    [self.view addSubview:searchView];
    
    //搜索矿所在的view
    UIView *inputView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 155, 30)];
    inputView.backgroundColor = [UIColor whiteColor];
    [searchView addSubview:inputView];
    inputView.layer.masksToBounds = YES;
    inputView.layer.cornerRadius = 15;
    
    UIImageView *searchImgaeView = [[UIImageView alloc]initWithFrame:CGRectMake(17, 6, 18, 18)];
    searchImgaeView.image = [UIImage imageNamed:@"chat_search"];
    [inputView addSubview:searchImgaeView];
    
    UITextField *searchField = [[UITextField alloc]initWithFrame:CGRectMake(45, 6, 100, 18)];
    searchField.placeholder = @"搜索";
    searchField.font = [UIFont systemFontOfSize:13];
    [inputView addSubview:searchField];
    
    UIButton* btn = [[UIButton alloc]initWithFrame:CGRectMake(180, 4, 22, 22)];
    //    btn.backgroundColor = [UIColor blueColor];
    [btn setImage:[UIImage imageNamed:@"chat_pushBtn"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(popToTongChatVCClick:) forControlEvents:UIControlEventTouchUpInside];
    [searchView addSubview:btn];
    
    SYTListViewController *ListVC = [[SYTListViewController alloc] init];
    ListVC.view.frame = CGRectMake(0, 130, 295, APP_HEIGHT - 130);
    ListVC.view.userInteractionEnabled = YES;

    ListVC.chooseChatManWithTagerId = ^(NSString * _Nonnull tagerId, NSString * _Nonnull userName) {
        [self setChatViewContollerWithTagerId:tagerId WithUserName:userName];

    };
    [self.view addSubview:ListVC.view];
    [self addChildViewController:ListVC];
    
    RYChatBaseView *vc = [[RYChatBaseView alloc]initWithFrame:CGRectMake(215, 20, APP_WIDTH-295, APP_HEIGHT - 20)];
    vc.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:vc];
}
-(void)setChatViewContollerWithTagerId:(NSString *)tagerID WithUserName:(NSString *)name{
    if (chatVC != nil) {
        [chatVC.view removeFromSuperview];
        chatVC = nil;
    }
        chatVC = [[RYChatViewController alloc] init];
        chatVC.conversationType = ConversationType_PRIVATE;
        chatVC.targetId = tagerID;
        chatVC.UserName = name;
        chatVC.view.frame = CGRectMake(215, 0, APP_WIDTH-295, APP_HEIGHT - 20);
        chatVC.view.userInteractionEnabled = YES;
        [self.view addSubview:chatVC.view];
        [self addChildViewController:chatVC];
}
-(void)popToTongChatVCClick:(UIButton *)sender{
    if (self.popToChatListView) {
        self.popToChatListView();
    }
}
@end
